<template>
  <div class="login-container ">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar " title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
        :rules="userRules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        maxlength="6"
        :rules="userRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- 倒计时 -->
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 5"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <!-- 发送验证码按钮 -->
          <van-button
            v-else
            native-type="button"
            class="send-sms-btn"
            size="mini"
            type="primary"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap" style="margin: 16px">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user';
// import axios from 'axios'
export default {
  name: 'login',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '',
        code: ''
      },
      // 验证码和手机号码的验证规则
      userRules: {
        mobile: [
          {
            required: true,
            message: '请填写手机号'
          },
          {
            pattern: /^1[3|4|5|6|7|8|9]\d{9}$/,
            message: '手机号错误'
          }
        ],
        code: [
          {
            required: true,
            message: '请填写验证码'
          },
          {
            pattern: /^\d{6}/,
            message: '验证码格式错误'
          }
        ]
      },
      // 验证码和倒计时互斥显示和隐藏的变量 通过V-if和V-else配合操作
      isCountDownShow: false
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit(values) {
      console.log('submit', values);

      // 轻提示-加载中
      this.$toast.loading({
        message: '登录中...',
        // 是否禁止背景点击: 是
        forbidClick: true,
        // 展示时长 - 单位为毫秒 - 设置为0不会消失
        duration: 0
      });

      try {
        // 通过axios请求通过await返回的数据结果
        const { data } = await login(this.user);
        //     const res = await axios({
        //         method: 'POST',
        // url: 'http://geek.itheima.net/v1_0/authorizations',
        // data: this.user
        //       })
        console.log(data);
        // vant-轻提示-成功提示
        this.$toast.success('登录成功');
        // 通过$store.commit调用全局的函数并传参
        this.$store.commit('setUser', data.data);
        // 登录成功后 返回
        this.$router.back()
      } catch (err) {
        console.log(err);
        if (err.response && err.response.status === 400) {
          // vant-轻提示-失败提示
          this.$toast.fail('手机号或验证码错误');
        } else {
          this.$toast.fail('登录失败，请稍后重试');
        }
      }
    },
    async onSendSms() {
      // 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile', 'code');
      } catch (err) {
        return console.log(err);
      }
      // 验证通过，显示倒计时
      this.isCountDownShow = true;
      // 请求发送验证码
      try {
        await sendSms(this.user.mobile);
        this.$toast('发送成功');
      } catch (error) {
        this.isCountDownShow = false;
        if (error.response.status === 429) {
          this.$toast('发送太频繁，请稍后重试');
        } else {
          this.$toast('发送失败,请稍后重试');
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .page-nav-bar {
    background-color: #3296fa;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    border-radius: 23px;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
      border-radius: 10px;
    }
  }
}
</style>
